<?php

class Suppliers extends Controller {
                  
	function Suppliers()
	{
		parent::Controller();	
        if(!admin_session_check()) redirect('admin/login');
        if(!is_allowed("all")) show_404();
        $this->load->model('productsmodel');
        $this->_process_form();          
	}     
    
    function index()
    {

        $data["list"] = $this->productsmodel->admin_get_all_suppliers();
        $data["response"] = get_response();        

        $this->load->view('admin/default/head');          
        $this->load->view('admin/default/header', array('selected'=>'products'));  
        $this->load->view("admin/default/nav_products", array('selected' => 'suppliers'));
        $this->load->view("admin/default/quick_search");
        $this->load->view('admin/suppliers/suppliers_list', $data);   
        $this->load->view('admin/default/footer');   

    }     

    function view($id = NULL)
    {
                                                
        if($id == NULL) redirect('admin/suppliers');

        $data["list"] = $this->productsmodel->admin_get_all_suppliers(); 
        $data["item"] = $this->productsmodel->admin_get_supplier_info($id);
        if(empty($data["item"])) show_404();
        $data["response"] = get_response();        

        $this->load->view('admin/default/head');          
        $this->load->view('admin/default/header', array('selected'=>'products'));  
        $this->load->view("admin/default/nav_products", array('selected' => 'suppliers'));
        $this->load->view("admin/default/quick_search");
        $this->load->view('admin/suppliers/suppliers_view', $data);   
        $this->load->view('admin/default/footer');   

    }    

    function edit($id = NULL, $standalone = false)
    {
                                                
        if($id == NULL) show_404();
        $data["list"] = $this->productsmodel->admin_get_all_suppliers(); 
        $data["item"] = $this->productsmodel->admin_get_supplier_info($id);
        if(empty($data["item"])) show_404();

        $data["token"] = generate_token("supplier",720);
        $data["action"] = "update";                                
        $data["standalone"] = $standalone;

        $this->load->view('admin/default/head');          
        if ($standalone == false){
            $this->load->view('admin/default/header', array('selected'=>'products'));  
            $this->load->view("admin/default/nav_products", array('selected' => 'suppliers'));
            $this->load->view("admin/default/quick_search");
        }
        $this->load->view('admin/suppliers/suppliers_edit', $data);   
        if ($standalone == false){
        $this->load->view('admin/default/footer');   
        }

    }
    function add($standalone = false)
    {

        $data["item"]['id_Supplier'] = null;
        $data["item"]['Name_Supplier'] = "";
        $data["item"]['Code_Supplier'] = "";
        $data["item"]['ContactName_Supplier'] = "";
        $data["item"]['Address_Supplier'] = "";
        $data["item"]['Phone_Supplier'] = "";
        $data["item"]['Fax_Supplier'] = "";
        $data["item"]['Mobile_Supplier'] = "";
        $data["item"]['Email_Supplier'] = "";
        $data["item"]['WebSite_Supplier'] = "";
        $data["item"]['City_Supplier'] = "";
        $data["item"]['Address2_Supplier'] = "";
        $data["item"]['State_Supplier'] = ""; 
        $data["item"]['Country_Supplier'] = ""; 
        $data["item"]['PostCode_Supplier'] = "";

        $data["token"] = generate_token("supplier",720); 
        $data["action"] = "create";                                       

        $data["standalone"] = $standalone;

        $this->load->view('admin/default/head');          
        if ($standalone == false) $this->load->view('admin/default/header', array('selected'=>'products'));  
        if ($standalone == false) $this->load->view("admin/default/nav_products", array('selected' => 'suppliers'));
        $this->load->view('admin/suppliers/suppliers_edit', $data);   
        if ($standalone == false) $this->load->view('admin/default/footer');   

    }
    function remove($id = false){

        if($id == NULL) show_404();  

        $data["products"] = $this->productsmodel->admin_get_supplier_products($id);

        if(!empty($data["products"])){

                set_response('error', "The supplier you're attempting to delete has ".count($data["products"])." product(s) associated to it. You must delete the products first or assign them to a different supplier.");
                redirect('admin/suppliers/view/'.$id);

        } else {

            $data["item"] = $this->productsmodel->admin_get_supplier_info($id);

            $this->load->view('admin/default/head');          
            $this->load->view('admin/default/header', array('selected'=>'products'));  
            $this->load->view("admin/default/nav_products", array('selected' => 'suppliers'));
            $this->load->view('admin/suppliers/suppliers_delete', $data);   
            $this->load->view('admin/default/footer');   

        }
    }
    function confirm_removal($id = false){

        if($id == NULL) show_404();  

        $data["item"] = $this->productsmodel->admin_get_supplier_info($id);
        $data["products"] = $this->productsmodel->admin_get_supplier_products($id);

        if(empty($data["item"])){

            set_response('error', "The supplier you're attempting to delete does not exist");
            redirect('admin/suppliers');

        } else if(!empty($data["products"])){

            set_response('error', "The supplier you're attempting to delete has ".count($data["products"])." product(s) associated to it. You must delete the products first or assign them them to a different supplier.");
            redirect('admin/suppliers/view/'.$id);

        } else {

            $this->productsmodel->admin_delete_supplier($id);
            set_response('success', "The supplier <i>".$data["item"]["Name_Supplier"]."</i> was deleted.");
            redirect('admin/suppliers');
        }
    }

    function _process_form()
    {

        $this->load->library("form_validation");       

        if($this->input->post("submit_action") !== FALSE){

            if ($this->_validate_form()){

                if (validate_token("supplier",$this->input->post("token",TRUE))){

                    $data = array(
                        'Name_Supplier' => $this->input->post("name"),  
                        'Code_Supplier' => $this->input->post("code"),
                        'ContactName_Supplier' => $this->input->post("contact_name"),
                        'Address_Supplier' => $this->input->post("address"), 
                        'Phone_Supplier' => $this->input->post("phone"),
                        'Fax_Supplier' => $this->input->post("fax"),
                        'Mobile_Supplier' => $this->input->post("mobile"),
                        'Email_Supplier' => $this->input->post("email"),
                        'WebSite_Supplier' => $this->input->post("website"),
                        'City_Supplier' => $this->input->post("city"),
                        'Address2_Supplier' => $this->input->post("address2"),
                        'Country_Supplier' => $this->input->post("country"),
                        'PostCode_Supplier' => $this->input->post("postcode")
                    );

                    $standalone = $this->input->post("standalone");

                    switch($this->input->post("submit_action")){    

                        case 'create':
                                                           
                            $new_id = $this->productsmodel->admin_insert_supplier($data);

                            if ($standalone == false){ 
                                set_response('success','Supplier <i>'.$data['Name_Supplier'].'</i> was created. <a href="'.site_url("admin/suppliers/edit/".$new_id).'">Click to edit</a>');
                                redirect('admin/suppliers/view/'.$new_id);
                            } else {
                                $this->_standalone_success($new_id);
                            }

                        break;
                        case 'update':

                            $this->productsmodel->update_supplier($this->input->post("supplier_id"), $data);

                            if ($standalone == false){
                                set_response('success','Supplier <i>'.$data['Name_Supplier'].'</i> was updated. <a href="'.site_url("admin/suppliers/edit/".$new_id).'">Click to re-edit</a>');
                                redirect('admin/suppliers/view/'.$this->input->post("supplier_id"));
                            } else {
                                $this->_standalone_success($this->input->post("supplier_id"));
                            }

                        break;        

                    }   

                }

            }

        }                         

    }

    private function _validate_form(){
        
        $config = array(
                        array(
                         'field'   => 'name',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean|required|max_length[250]'
                        ),
                        array(
                         'field'   => 'code',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean|max_length[200]'
                        ),
                        array(
                         'field'   => 'contact_name',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean|max_length[250]'
                        ),             
                        array(
                         'field'   => 'address',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean'
                        ),
                        array(
                         'field'   => 'phone',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean|max_length[100]'
                        ),
                        array(
                         'field'   => 'fax',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean|max_length[100]'
                        ),
                        array(
                         'field'   => 'mobile',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean|max_length[100]'
                        ),
                        array(
                         'field'   => 'email',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean|valid_email|max_length[200]'
                        ),
                        array(
                         'field'   => 'website',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean|max_length[200]'
                        ),
                        array(
                         'field'   => 'city',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean|max_length[200]'
                        ),
                        array(
                         'field'   => 'state',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean|max_length[200]'
                        ),
                        array(
                         'field'   => 'address2',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean'
                        ),
                        array(
                         'field'   => 'postcode',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean|max_length[200]'
                        ),
                        array(
                         'field'   => 'country',
                         'label'   => '',
                         'rules'   => 'trim|xss_clean|max_length[200]'
                        )
                    );
        
                                                 
        $this->form_validation->set_rules($config);       
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        
        if ($this->form_validation->run($this) == FALSE){
            return FALSE;
        }else
            return TRUE;

    }

    private function _standalone_success($id=false){
               
        $this->load->view('admin/default/head');          
        $this->load->view('admin/suppliers/suppliers_standalone_success', array("id" => $id));
                 
    } 

    public function export(){                                                         
        $this->load->plugin('to_excel');

        $this->db->select('*');
        $this->db->from('supplier');
        // run joins, order by, where, or anything else here
        $query = $this->db->get();
        to_excel($query, 'suppliers_pc_database');
    }


    function deactivate_product($product_id = false, $supplier_id = false){
                                   
        if($product_id == NULL) show_404();                                         
        $data = array('Active_Products' => 0);
        $this->productsmodel->update_product($product_id, $data);
        $item = $this->productsmodel->admin_get_product($product_id);
        set_response('success', "The product <i>".$item["Name_Products"].'</i> is now inactive. <a href="'.site_url("admin/products/preview/".$item["id_Products"]).'" rel="view">Click to view</a>');
        redirect('admin/suppliers/view/'.$supplier_id);
    }
    function activate_product($product_id = false, $supplier_id = false){

        if($product_id == NULL) show_404();  
        $data = array('Active_Products' => 1);
        $this->productsmodel->update_product($product_id, $data);
        $item = $this->productsmodel->admin_get_product($product_id);
        set_response('success', "The product <i>".$item["Name_Products"].'</i> is now active. <a href="'.site_url("admin/products/preview/".$item["id_Products"]).'" rel="view">Click to view</a>');
        redirect('admin/suppliers/view/'.$supplier_id);
    }
    function approve_product($product_id = false, $supplier_id = false){

        if(!is_allowed(array(ROLE_ADMIN,ROLE_PUBLISHER))) show_404();
        if($product_id == NULL) show_404();  

        $data = array('Approved_Products' => 1,
                    'Active_Products' => 1);
        $this->productsmodel->update_product($product_id, $data);
        $item = $this->productsmodel->admin_get_product($product_id);
        set_response('success', "The product <i>".$item["Name_Products"].'</i> was approved. <a href="'.site_url("admin/products/preview/".$item["id_Products"]).'" rel="view">Click to view</a>');
        redirect('admin/suppliers/view/'.$supplier_id);
    }
     
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */